package com.gxa.financial.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.common.R;
import com.gxa.common.entity.financial_management.WithdrawalManagement;
import com.gxa.common.entity.financial_management.WithdrawalSetting;
import com.gxa.financial.mapper.WithdrawalManagementMapper;
import com.gxa.financial.service.AccountListService;
import com.gxa.financial.service.WithdrawalManagementService;
import com.gxa.financial.service.WithdrawalSettingService;
import com.gxa.financial.utils.ExportExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;

@Service
public class WithdrawalManagementServiceImpl extends ServiceImpl<WithdrawalManagementMapper, WithdrawalManagement> implements WithdrawalManagementService {

    @Autowired
    private WithdrawalManagementMapper withdrawalManagementMapper;

    @Cacheable(value = "WithdrawalList",key = "'selectWithdrawalList'")
    @Override
    public R selectWithdrawalList(Integer currentPage, Integer pageSize) {
        Page page = new Page(currentPage,pageSize);
        Page selectPage = withdrawalManagementMapper.selectPage(page, null);
        R r = new R();
        if (selectPage.getRecords() != null) {
            Map WithdrawalList = new HashMap();
            WithdrawalList.put("totalSize",selectPage.getTotal());
            WithdrawalList.put("totalPage",selectPage.getPages());
            WithdrawalList.put("totalData",selectPage.getRecords());
            r.setCode(200);
            r.setMessage("查询成功");
            r.setData(WithdrawalList);
        }else {
            r.setCode(500);
            r.setMessage("查询失败");
        }
        return r;
    }

    @Override
    public R selectWithdrawal(Integer currentPage, Integer pageSize, WithdrawalManagement withdrawalManagement) {
        Page page = new Page(currentPage,pageSize);
        QueryWrapper qw = new QueryWrapper();
        qw.eq(withdrawalManagement.getTransactionNo() != null,"transaction_no",withdrawalManagement.getTransactionNo())
                .eq(withdrawalManagement.getAccountType() != null,"account_type",withdrawalManagement.getAccountType())
                .eq(withdrawalManagement.getApplicant() != null,"applicant",withdrawalManagement.getApplicant())
                .eq(withdrawalManagement.getAccPhone() != null,"phone",withdrawalManagement.getAccPhone())
                .eq(withdrawalManagement.getBankCardNo() != null,"status",withdrawalManagement.getBankCardNo())
                .eq(withdrawalManagement.getStatus() != null,"status",withdrawalManagement.getStatus())
                .between(withdrawalManagement.getStartTime() != null&&withdrawalManagement.getEndTime() != null,"create_time",withdrawalManagement.getStartTime(),withdrawalManagement.getEndTime())
                .lt(withdrawalManagement.getStartTime() == null && withdrawalManagement.getEndTime() != null,"create_time",withdrawalManagement.getEndTime());
        Page selectPage = withdrawalManagementMapper.selectPage(page, qw);
        R r = new R();
        if (selectPage.getRecords() != null) {
            Map accountListMap = new HashMap();
            accountListMap.put("totalSize",selectPage.getTotal());
            accountListMap.put("totalPage",selectPage.getPages());
            accountListMap.put("totalData",selectPage.getRecords());
            r.setCode(200);
            r.setMessage("查询成功");
            r.setData(accountListMap);
        }else {
            r.setCode(500);
            r.setMessage("查询失败");
        }
        return r;
    }

    @Override
    public R addWithdrawal(WithdrawalManagement withdrawalManagement) {
        withdrawalManagement.setApplyTime(new Date());//添加当前申请时间
        WithdrawalSettingService withdrawalSettingService = new WithdrawalSettingServiceImpl();
        //根据传递的用户类型计算手续费以及打款金额
        R setting = withdrawalSettingService.selectWithdrawalSetting(withdrawalManagement.getAccountType());
        WithdrawalSetting withdrawalSetting = (WithdrawalSetting)setting.getData();
        BigDecimal rate = new BigDecimal(withdrawalSetting.getWithdrawalRate());//计算的费率
        withdrawalManagement.setPoundage(withdrawalManagement.getWithdrawalAmount().multiply(rate));//设置手续费
        withdrawalManagement.setPaymentAmount(withdrawalManagement.getWithdrawalAmount().subtract(withdrawalManagement.getPoundage()));//打款金额
        int insert = withdrawalManagementMapper.insert(withdrawalManagement);
        //扣取账户余额
        AccountListServiceImpl accountListServiceImpl = new AccountListServiceImpl();
        int i = accountListServiceImpl.updateAccountBalance(withdrawalManagement.getAccPhone(),withdrawalManagement.getWithdrawalAmount());
        R r = new R();
        if (insert > 0 && i > 0) {
            r.setCode(200);
            r.setMessage("提现中");
        }else {
            r.setCode(500);
            r.setMessage("数据失败");
        }
        return r;
    }

    @Override
    public R selectWithdrawalByNo(Long transactionNo) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("transaction_no",transactionNo);
        WithdrawalManagement withdrawalManagement = withdrawalManagementMapper.selectOne(qw);
        R r = new R();
        if (withdrawalManagement != null) {
            r.setCode(200);
            r.setMessage("查询成功");
            r.setData(withdrawalManagement);
        }else {
            r.setCode(500);
            r.setMessage("查询失败");
        }
        return r;
    }

    @Override
    public R updateWithdrawalByNo(WithdrawalManagement withdrawalManagement) {
        withdrawalManagement.setProcessTime(new Date());
        int i = withdrawalManagementMapper.updateById(withdrawalManagement);//id,处理人，原因
        R r = new R();
        if (i > 0) {
            r.setCode(200);
            r.setMessage("修改成功");
            r.setData(withdrawalManagement);
        }else {
            r.setCode(500);
            r.setMessage("修改失败");
        }
        return r;
    }


    @Override
    public void export(HttpServletRequest request, HttpServletResponse response) {
        Map map = new HashMap();
        map.put("status",0);
        List<WithdrawalManagement> select = withdrawalManagementMapper.selectByMap(map);
        //创建表都
        List<String> column = new ArrayList<>();
        column.add("交易号");
        column.add("申请人");
        column.add("申请人电话");
        column.add("提现方式");
        column.add("银行卡号");
        column.add("预留手机号");
        column.add("提现金额");
        column.add("打款金额");
        column.add("手续费");
        column.add("申请时间");

        //表头对应的数据
        List<Map<String,Object>> data = new ArrayList<>();

        //遍历获取到的需要导出的数据，k要和表头一样
        for (int i = 0; i < select.size(); i++) {
            Map<String,Object> dataMap = new HashMap<>();
            WithdrawalManagement withdrawalManagement = select.get(i);
            dataMap.put("交易号",withdrawalManagement.getTransactionNo());
            dataMap.put("申请人",withdrawalManagement.getApplicant());
            dataMap.put("申请人电话",withdrawalManagement.getAccPhone());
            dataMap.put("提现方式",withdrawalManagement.getWithdrawalMethod());
            dataMap.put("银行卡号",withdrawalManagement.getBankCardNo());
            dataMap.put("预留手机号",withdrawalManagement.getReservePhone());
            dataMap.put("提现金额",withdrawalManagement.getWithdrawalAmount());
            dataMap.put("打款金额",withdrawalManagement.getPaymentAmount());
            dataMap.put("手续费",withdrawalManagement.getPoundage());
            dataMap.put("申请时间",withdrawalManagement.getApplyTime());

            data.add(dataMap);
        }

        //调用导出工具类
        ExportExcel.exportExcel("提现待办表",column,data,request,response);
    }
}
